﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TempCtr.Model;

namespace TempCtr
{
    #pragma warning disable IDE1006 // 命名样式
    public partial class equip_manage : Form
    {
        public equip_manage()
        {
            InitializeComponent();
        }

        private void equip_manage_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Application.Exit();
            this.Dispose();
        }

        private void equip_manage_Load(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < Thermostat.ccck; i++)
                {
                    comboBox1.Items.Add(i + 1);
                }
                comboBox1.SelectedIndex = 0;
                gridview_Display(comboBox1.SelectedIndex + 1);
            }
            catch
            {

            }
                 
        }

        private void gridview_Display(int xnum)
        {
            dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            DataTable DT = MySqlHelp.GetThermostat(xnum);
            dataGridView1.DataSource = DT;

            dataGridView1.RowHeadersVisible = false;                                                         //删除第一列空白列
            dataGridView1.Columns[0].HeaderText = "温控器ID";
            dataGridView1.Columns[1].HeaderText = "柜编号";
            dataGridView1.Columns[2].HeaderText = "COM口";
            dataGridView1.Columns[3].HeaderText = "温控器地址";
            dataGridView1.Columns[4].HeaderText = "温控器状态";
            dataGridView1.Columns[5].Visible = false;

            textBox3.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
            label4.Text= dataGridView1.Rows[0].Cells[3].Value.ToString();
            label5.Text= dataGridView1.Rows[0].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[0].Cells[1].Value.ToString();

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {            
            label4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
            label5.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();

            if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString() == "1")
            {
                button1.Text = "停用";
            }
            else
            {
                button1.Text = "启用";
            }
        }

        private void Bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (MySqlHelp.UPDATEThermostatNum(int.Parse(label5.Text),textBox2.Text) != 0)
                {
                    gridview_Display(comboBox1.SelectedIndex + 1);
                    MainFrom.isUpdate = true;
                }
            }
            catch
            {

            }
           
        }

        private void Bt_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (dataGridView1.RowCount >= 2)
                {
                    if (MySqlHelp.DELETEThermostat(int.Parse(dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value.ToString())) != 0)
                    {
                        gridview_Display(comboBox1.SelectedIndex + 1);
                        MainFrom.isUpdate = true;
                    }
                }
                else
                {
                    MessageBox.Show("不能删除第一列");
                }
                
            }
            catch(Exception ex)
            {
                MainFrom.Logger.Error("delete error"+ex.ToString());
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "启用")
            {
                try
                {
                    if (MySqlHelp.SWITCHhermostat(int.Parse(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString()), "1") != 0)
                    {
                        DataTable DT = MySqlHelp.GetThermostat();
                        dataGridView1.DataSource = DT;
                        button1.Text = "停用";
                    }
                }
                catch
                {

                }
            }
            else
            {
                try
                {
                    if (MySqlHelp.SWITCHhermostat(int.Parse(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString()), "0") != 0)
                    {
                        DataTable DT = MySqlHelp.GetThermostat();
                        dataGridView1.DataSource = DT;
                        button1.Text = "启用";
                    }
                }
                catch
                {

                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value.ToString())+1;
            int addr = int.Parse(dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[3].Value.ToString()) + 1;
            if (addr <= 32)
            {
                if (MySqlHelp.ADDThermostat(id, textBox3.Text, addr, comboBox1.SelectedIndex + 1) != 0)
                {
                    MainFrom.isUpdate = true;
                    gridview_Display(comboBox1.SelectedIndex + 1);
                }
            }
            else
            {
                MessageBox.Show("数量超过最大限制！"); 
            }
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridview_Display(comboBox1.SelectedIndex + 1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (MySqlHelp.UPDATEThermostatCom(comboBox1.SelectedIndex + 1, textBox3.Text) != 0)
                {
                    MainFrom.isUpdate = true;
                    gridview_Display(comboBox1.SelectedIndex + 1);
                }
            }
            catch
            {

            }
        }
    }
}
